package com.lancoo.znbkxx.teacher.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.Animatable
import android.net.Uri
import android.text.SpannableString
import android.text.style.LeadingMarginSpan
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.controller.BaseControllerListener
import com.facebook.imagepipeline.image.ImageInfo
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.ChildOption
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.lancoo.znbkxx.teacher.utils.HtmlUtils
import kotlinx.android.synthetic.main.item_question_choice_analysis.view.*
import java.util.regex.Pattern

/**
 * Created by wealive on 2021/3/2.
 *
 *
 */
class ChoiceAnalysisAdapter(val type: String, data: MutableList<ChildOption>) :
        BaseQuickAdapter<ChildOption, BaseViewHolder>(data) {
    init {
        mLayoutResId = R.layout.item_question_choice_analysis
    }

    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder, item: ChildOption?) {
        with(helper.itemView) {
            val content = item!!.Option.trim()
            if (content.contains("src")) {
                tv_choice.text = "${item.OptionIndex}."
                sdv.visibility = View.VISIBLE
                val matcher = Pattern.compile(
                        "((http|ftp|https)://)(([a-zA-Z0-9._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/?[a-zA-Z0-9&%_./-~-]*)?",
                        Pattern.CASE_INSENSITIVE
                ).matcher(content)
                while (matcher.find()) {
                    val path = matcher.group(0)
                    val controllerListener = Fresco.newDraweeControllerBuilder()
                        .setControllerListener(object : BaseControllerListener<ImageInfo>() {
                            override fun onFinalImageSet(id: String?, imageInfo: ImageInfo?,
                                                         animatable: Animatable?) {
                                val params = sdv.layoutParams
                                params.height = DensityUtils.dp2px(context, 100f)
                                params.width = ((imageInfo!!.width.toFloat() / imageInfo.height * DensityUtils.dp2px(
                                        context, 100f)).toInt())
                                sdv.layoutParams = params
                            }
                        }).setUri(Uri.parse(path)).build()
                    sdv.controller = controllerListener
                }
            } else {
                tv_choice.text = HtmlUtils.setText("${item.OptionIndex}.${content}")
                val spannable = SpannableString(tv_choice.text)
                val what = LeadingMarginSpan.Standard(0, DensityUtils.dp2px(context, 15f))
                spannable.setSpan(
                        what,
                        0,
                        spannable.length,
                        SpannableString.SPAN_INCLUSIVE_INCLUSIVE
                )
                tv_choice.text = spannable
                sdv.visibility = View.GONE
            }
            if (item.isAnswer && type != "k") {
                tv_choice.setTextColor(Color.parseColor("#00afff"))
            } else {
                tv_choice.setTextColor(Color.parseColor("#333333"))
            }
        }
    }
}